<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>我的登记</span>
      </div>
    </template>

    <!-- 数据表格 -->
    <el-table
      :data="tableData"
      v-loading="loading"
      border
      stripe
    >
      <el-table-column label="学年" align="center" header-align="center" width="130">
        <template #default="scope">
          {{ formatAcademicYear(scope.row.academicYear) }}
        </template>
      </el-table-column>
      <el-table-column label="衣服款式" align="center" header-align="center" width="180">
        <template #default="scope">
          <div class="style-cell">
            <el-image 
              v-if="scope.row.styleInfo && scope.row.styleInfo.image" 
              :src="scope.row.styleInfo.image" 
              fit="cover"
              class="style-image"
              :preview-src-list="[scope.row.styleInfo.image]"
            />
            <div v-else class="style-image-placeholder">暂无图片</div>
            <div class="style-name">{{ scope.row.styleInfo ? scope.row.styleInfo.name : '-' }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="性别限制" align="center" header-align="center" width="100">
        <template #default="scope">
          <el-tag 
            v-if="scope.row.styleInfo"
            size="small" 
            :type="scope.row.styleInfo.genderLimit === 0 ? 'danger' : scope.row.styleInfo.genderLimit === 1 ? 'primary' : 'info'"
          >
            {{ formatGenderLimit(scope.row.styleInfo.genderLimit) }}
          </el-tag>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="尺码" align="center" header-align="center" width="120">
        <template #default="scope">
          <el-tag type="warning">{{ scope.row.size || '-' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="尺码说明" align="center" header-align="center" min-width="200">
        <template #default="scope">
          {{ scope.row.sizeDescription || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="登记截止时间" align="center" header-align="center" width="180">
        <template #default="scope">
          {{ formatDateTime(scope.row.registerDeadline) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" fixed="right" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="handleDetail(scope.row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 空数据提示 -->
    <el-empty v-if="!loading && tableData.length === 0" description="暂无登记记录" />

    <!-- 查看详情对话框 -->
    <el-dialog v-model="detailVisible" title="登记详情" width="700px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="学年">
          {{ formatAcademicYear(detailData.academicYear) }}
        </el-descriptions-item>
        <el-descriptions-item label="登记截止时间">
          {{ formatDateTime(detailData.registerDeadline) }}
        </el-descriptions-item>
        <el-descriptions-item label="衣服款式" :span="2">
          <div class="detail-style">
            <el-image 
              v-if="detailData.styleInfo && detailData.styleInfo.image" 
              :src="detailData.styleInfo.image" 
              fit="cover"
              class="detail-style-image"
              :preview-src-list="[detailData.styleInfo.image]"
            />
            <div v-else class="detail-style-image-placeholder">暂无图片</div>
            <div class="detail-style-info">
              <div class="detail-style-name">{{ detailData.styleInfo ? detailData.styleInfo.name : '-' }}</div>
              <el-tag 
                v-if="detailData.styleInfo"
                size="small" 
                :type="detailData.styleInfo.genderLimit === 0 ? 'danger' : detailData.styleInfo.genderLimit === 1 ? 'primary' : 'info'"
              >
                {{ formatGenderLimit(detailData.styleInfo.genderLimit) }}
              </el-tag>
            </div>
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="尺码">
          <el-tag type="warning">{{ detailData.size || '-' }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="尺码说明">
          {{ detailData.sizeDescription || '-' }}
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button @click="detailVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { _getMyRegistration } from '@/api/registration'
import { _getClothingStyleById } from '@/api/clothingStyle'

// 响应式数据
const loading = ref(false)
const tableData = ref([])
const detailVisible = ref(false)
const detailData = ref({})

// 格式化学年
const formatAcademicYear = (year) => {
  if (!year) return '-'
  return `${year}学年`
}

// 格式化性别限制
const formatGenderLimit = (genderLimit) => {
  if (genderLimit === 0) return '女款'
  if (genderLimit === 1) return '男款'
  if (genderLimit === 2) return '通用'
  return '-'
}

// 格式化日期时间
const formatDateTime = (dateTime) => {
  if (!dateTime) return '-'
  const date = new Date(dateTime)
  if (isNaN(date.getTime())) return '-'
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

// 获取登记列表
const getList = async () => {
  loading.value = true
  try {
    const res = await _getMyRegistration()
    if (res && res.code === 200) {
      const registrationList = res.data.registrationVoList || []
      
      // 为每个登记项获取衣服款式信息
      const listWithStyleInfo = await Promise.all(
        registrationList.map(async (registration) => {
          if (registration.style_id) {
            try {
              const styleRes = await _getClothingStyleById(registration.style_id)
              if (styleRes && styleRes.code === 200) {
                return {
                  ...registration,
                  styleInfo: styleRes.data.clothingStyle
                }
              }
            } catch (error) {
              console.error('获取款式信息失败:', error)
            }
          }
          return {
            ...registration,
            styleInfo: null
          }
        })
      )
      
      tableData.value = listWithStyleInfo
    } else {
      ElMessage.error(res.message || '获取登记列表失败')
    }
  } catch (error) {
    console.error('获取登记列表失败:', error)
    ElMessage.error('获取登记列表失败')
  } finally {
    loading.value = false
  }
}

// 查看详情
const handleDetail = (row) => {
  detailData.value = { ...row }
  detailVisible.value = true
}

// 初始化
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  
  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 18px;
    font-weight: bold;
  }
}

// 表格中的款式展示
.style-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  
  .style-image {
    width: 80px;
    height: 80px;
    border-radius: 4px;
    cursor: pointer;
  }
  
  .style-image-placeholder {
    width: 80px;
    height: 80px;
    border-radius: 4px;
    background-color: #f5f7fa;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #909399;
    font-size: 12px;
  }
  
  .style-name {
    font-size: 14px;
    font-weight: 500;
    color: #303133;
    text-align: center;
  }
}

// 详情对话框中的款式展示
.detail-style {
  display: flex;
  align-items: center;
  gap: 16px;
  
  .detail-style-image {
    width: 120px;
    height: 120px;
    border-radius: 8px;
    cursor: pointer;
    flex-shrink: 0;
  }
  
  .detail-style-image-placeholder {
    width: 120px;
    height: 120px;
    border-radius: 8px;
    background-color: #f5f7fa;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #909399;
    font-size: 14px;
    flex-shrink: 0;
  }
  
  .detail-style-info {
    display: flex;
    flex-direction: column;
    gap: 8px;
    
    .detail-style-name {
      font-size: 16px;
      font-weight: 600;
      color: #303133;
    }
  }
}
</style>
